package com.resou.reader.mine.consumerecord;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.api.entry.ConsumptionDetailBean;
import com.resou.reader.utils.log.DLog;
import com.resou.reader.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${Garrett} on 2018/9/21.
 * Contact me krouky@outlook.com
 */
public class ConsumeAdapter extends RecyclerView.Adapter {

    private SparseArray<ConsumptionDetailBean> firstBeanSparseArray = new SparseArray<>();//存储每日流水数据
    private SparseArray<ConsumptionDetailBean.ConsumptionItemsBean> secondBeanSparseArray = new SparseArray<>();//存储每条流水数据

    private List<ConsumptionDetailBean> mList = new ArrayList<>();
    private Context mContext;
    private boolean hasMore = true;//是否有下一页

    private int normalType = 0;
    //    private int secondNormalType = 2;
    private int footType = 1;

    public ConsumeAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void updateList(List<ConsumptionDetailBean> beans, boolean hasMore) {
        boolean temp = (!this.hasMore && hasMore) || (this.hasMore && !hasMore);
        this.hasMore = hasMore;
        if (beans != null && beans.size() > 0) {
            mList.addAll(beans);
            for (int i = 0; i < mList.size(); i++) {
                firstBeanSparseArray.put(i, mList.get(i));
            }
            notifyDataSetChanged();
            DLog.d(ConsumeAdapter.class.getSimpleName(), "notifyDataSetChanged");
        } else if (temp) {
            notifyItemChanged(getItemCount() - 1);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == normalType) {
            return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_consume_layout, viewGroup, false));
        } else {
            return new FootHolder(LayoutInflater.from(mContext).inflate(R.layout.recycler_foot_layout, viewGroup, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (getItemViewType(i) == normalType) {
            ViewHolder ranViewHolder = (ViewHolder) viewHolder;
            ConsumptionDetailBean bean = firstBeanSparseArray.get(i);// mList.get(i);
            if (bean == null) {
                return;
            }
            ranViewHolder.bookNameText.setText(bean.getBookName() + "");
            ranViewHolder.consumeTimeText.setText("购买于" + DateUtils.getTheDayFromNow(bean.getConsumeTimeMills() * 1000));
            if (firstBeanSparseArray.get(i).getExpand()) {
                ranViewHolder.mRecyclerView.setVisibility(View.VISIBLE);
                ranViewHolder.statusImage.setImageResource(R.mipmap.ic_expand_status);
                ranViewHolder.mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                ConsumechildAdapter consumechildAdapter = new ConsumechildAdapter(mContext);
                ranViewHolder.mRecyclerView.setAdapter(consumechildAdapter);
                consumechildAdapter.updata(mList.get(i).getConsumptionItems());
            } else {
                ranViewHolder.statusImage.setImageResource(R.mipmap.ic_collapsing_status);
                ConsumechildAdapter consumechildAdapter = new ConsumechildAdapter(mContext);
                ranViewHolder.mRecyclerView.setAdapter(consumechildAdapter);
                consumechildAdapter.updata(mList.get(i).getConsumptionItems());
                ranViewHolder.mRecyclerView.setVisibility(View.GONE);
            }
            int voucherUsed = bean.getVoucherUse();//使用的热券
            int currencyUsed = bean.getCurrencyUse();//使用的热币
            final String voucherUsedStr = String.valueOf(voucherUsed);
            final String currencyUsedStr = String.valueOf(currencyUsed);
            final SpannableStringBuilder ssb = new SpannableStringBuilder();
            final int themeColor = mContext.getResources().getColor(R.color.theme_color);
            final ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(themeColor);
            if (voucherUsed == 0) {
                ssb.append(currencyUsedStr);
                ssb.setSpan(foregroundColorSpan, 0, currencyUsedStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.append(" 热币");
            } else if (currencyUsed == 0) {
                ssb.append(voucherUsedStr);
                ssb.setSpan(foregroundColorSpan, 0, voucherUsedStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.append(" 热券");
            } else {
                final String addStr = " + ";

                ssb.append(voucherUsedStr);
                ssb.setSpan(foregroundColorSpan, 0, voucherUsedStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.append(" 热券");
                ssb.append(addStr);
                ssb.append(currencyUsedStr);
                ssb.setSpan(new ForegroundColorSpan(themeColor), ssb.length() - currencyUsedStr.length(), ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.append(" 热币");
            }
            ranViewHolder.consumeNumText.setText(ssb);

            ranViewHolder.mLinearOnclick.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (firstBeanSparseArray.get(i).isExpand()) {
                        //设置第二级布局是否展开的flag
                        firstBeanSparseArray.get(i).setExpand(false);
                        notifyDataSetChanged();
                    } else {
                        //设置第二级布局是否展开的flag
                        firstBeanSparseArray.get(i).setExpand(true);
                        notifyDataSetChanged();
                    }
                }
            });
        } else {
            FootHolder footHolder = (FootHolder) viewHolder;
            footHolder.moreText.setVisibility(View.VISIBLE);
            if (hasMore) {
                footHolder.moreText.setText("加载更多~");
            } else {
                footHolder.moreText.setText("没有更多了~");
            }
        }
    }

    @Override
    public int getItemCount() {
//        return mList.size() + 1;
        return firstBeanSparseArray.size() + secondBeanSparseArray.size() + 1;
    }


    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return footType;
        } else {
            return normalType;
        }
    }

    public void clear() {
        mList.clear();
        firstBeanSparseArray.clear();
        secondBeanSparseArray.clear();
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView bookNameText;
        TextView consumeTimeText;
        TextView consumeNumText;
        ImageView statusImage;
        RecyclerView mRecyclerView;
        LinearLayout mLinearOnclick;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mLinearOnclick = itemView.findViewById(R.id.mLinearOnclick);
            bookNameText = itemView.findViewById(R.id.bookName);
            consumeTimeText = itemView.findViewById(R.id.consume_time);
            consumeNumText = itemView.findViewById(R.id.consume_num);
            mRecyclerView = itemView.findViewById(R.id.mRecyclerView);
            statusImage = itemView.findViewById(R.id.status_image);

        }
    }

    class FootHolder extends RecyclerView.ViewHolder {

        TextView moreText;

        FootHolder(@NonNull View itemView) {
            super(itemView);
            moreText = itemView.findViewById(R.id.recycler_foot_text);
        }
    }

}
